using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;
using AjaxControlToolkit;
using System.IO;

public partial class testprint : System.Web.UI.Page
{
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {

        string strPagesetRowID = Request.QueryString["pgs_id"];
        //string strmnuid = 
        string strkeyvalue = Request.QueryString["PK"];

        setPageTitle();
        setLogo();

        security hdds = new security();
        //Code added by Sampath on 15-Nov-2011 for IPP Vertical privilege
        if (Request.QueryString["PK"] != null && Request.QueryString["pgs_id"] != null)
        {
            if (!hdds.checkRecordAccess(Request.QueryString["PK"].ToString(), Request.QueryString["pgs_id"].ToString()))
            {
                lblmessage.Text = wrapItAsMessage("<img src=\"Images/info.gif\" alt=\"\"></img>&nbsp;You do not have sufficient access permission.", true);
                tdmain.Visible = false;
                ltrl_logo.Visible = false;
                tdDisplayGrid.Visible = false;
                tdcPage.Visible = false;                
                return;
            }
        }
        hdds = null;
        Master MA = new Master();
        if (MA.DeleteMessage != "")
        {
            //lbl_message.Text = MA.DeleteMessage;
            MA.DeleteMessage = "";
        }
       
            //lbl_message.Text = MA.DeleteMessage;
        DynamicDisplayI DDI = new DynamicDisplayI();
        PageInfoLoad PIL = new PageInfoLoad();
        //DataSet ds = new DataSet();
        ds= PIL.GetPageSetPageCtrlInfo(strPagesetRowID);
        DDI.Load(strPagesetRowID,form1,ref ds,null,null);


        if (Request.QueryString["CPY"] == null)
        {
            if (Request.QueryString["PK"] != null)
            {
                DataTable dtChilds = DDI.getAllChildObjects(strPagesetRowID);
                if (dtChilds != null)//implemenetd by sanal on April 03, 2008
                    if (dtChilds.Rows.Count > 0)
                    {
                        HttpContext.Current.Session["PK"] = HttpContext.Current.Request.QueryString["PK"].ToString();
                        //Navigator.storeURL(Request.Url.ToString(), IsPostBack, false, false);
                        DDI.getPagesetandLoad(dtChilds, form1);
                    }

                //putPrintButtons(DDI);
            }
        }


        //DDI.FindControl(form1.Controls, "tdcpage"))


        


        CheckEditOperation();
        //replacecontrols(form1);
        findtable(form1,ds);
    }

    private bool CheckEditOperation()
    {
        bool blnStatus = true;
        string strPageKey = "";
        bool IsEdit = false;
        //if (Page.IsPostBack == false)
        //{
        if (Request.QueryString["PK"] != null)
        {
            strPageKey = Request.QueryString["PK"].ToString();
            IsEdit = true;
        }
        if (IsEdit)
        {
            Master MA = new Master();
            blnStatus = MA.EditOperation(ds, form1, strPageKey);
            if (blnStatus == false)
            {
                //strErrMessage = MA.ErrorMessage;
               // Response.Write("<font color='red'>Error : " + strErrMessage + "</font>");
            }
        }
        //}
        return blnStatus;
    }

   private void supressSectionHeader(ref HtmlTable ht) //Added By Dhanapal On Dec 17 2010 For Section header Supression
    {
        //foreach (HtmlTableRow htr in ht.Rows)
        for (  int index = 0; index < ht.Rows.Count ; index++ )
        {
            HtmlTableRow htr = ht.Rows[index];
            string id = htr.ID.Trim();
            if (id != "" || id != null)
            {
               if (id.IndexOf("__Child") == -1) //TO Check Whether it is a Section Header or Not
              
                {
                    ht.Controls.Remove(htr); //Remove the Section Header From the main Table

                }

            }
        }
    }

    private void findtable(HtmlForm frmobj,DataSet ds)
    {

        string tablename = "";
        Literal litspace = new Literal();
        litspace.Text ="&nbsp;";
        if (ds != null)
        {
            string page_id = ds.Tables["page"].Rows[0]["page_id"].ToString();
            tablename = "tbl_"+page_id;
        }
        
        try
        {
            HtmlTable table = (HtmlTable)FindControl(tablename);
            table.Width = "100%";
            supressSectionHeader(ref table);//Added By Dhanapal On Dec 17 2010 For Section Header Supression
            
            foreach (HtmlTableRow htr in table.Rows)
            {
               
                foreach (HtmlTableCell htc in htr.Cells)
                {
                    if (htc.HasControls())
                    {
                        for (int i = 0; i < htc.Controls.Count; i++)
                        {
                            if (htc.Controls[i] is Label)
                            {
                                Label lbl1 = (Label)htc.Controls[i];
                                lbl1.CssClass = "defaultFont";
                                lbl1.Font.Size = FontUnit.Point(9);
                                string labelid = lbl1.ID;
                                if (labelid != null)
                                {
                                 if(labelid.ToLower().Contains("lb_p_"))
                                 {

                                    lbl1.Font.Bold = true;
                                 }
                                 if (lbl1.Text.ToLower().Contains("<a"))
                                 {
                                     lbl1.Visible = false;
                                 }
                                 if (lbl1.Text.ToLower().Contains("<img"))
                                 {
                                     lbl1.Visible = false;
                                 }
                                 if (lbl1.ID.ToLower().Contains("img_search_popup"))
                                 {
                                     lbl1.Visible = false;
                                 }

                                }
                                //lbl1.CssClass = "controlFont";
                                htc.Controls.Add(litspace);
                            }
                            if (htc.Controls[i] is TextBox)
                            {
                                Label mylabel = new Label();
                                //mylabel.CssClass = "controlFont";
                                mylabel.CssClass = "defaultFont";
                                mylabel.Font.Size = FontUnit.Point(9);
                                TextBox txt = (TextBox)htc.Controls[i];
                                txt.Visible = false;
                                mylabel.Text = HttpUtility.HtmlEncode(txt.Text);
                                //htc.Controls.Remove(txt);
                                htc.Controls.Add(mylabel);
                                htc.Controls.Add(litspace);
                            }
                            if (htc.Controls[i] is RequiredFieldValidator)
                            {
                                RequiredFieldValidator reqval = (RequiredFieldValidator)htc.Controls[i];

                                string strreqval = reqval.ID;
                                if (strreqval != null)
                                {
                                    if (strreqval.ToLower().Contains("rfv_"))
                                    {
                                        htc.Controls.Remove(reqval);
                                    }
                                }

                               
                            }
                            if (htc.Controls[i] is ValidatorCalloutExtender)
                            {
                                ValidatorCalloutExtender valcall = (ValidatorCalloutExtender)htc.Controls[i];
                                string strvalcall = valcall.ID;
                                if (strvalcall != null)
                                {
                                    if (strvalcall.ToLower().Contains("vce_"))
                                    {
                                        htc.Controls.Remove(valcall);
                                    }
                                }
                            }
                            if (htc.Controls[i] is Image)
                            {
                                Image img = (Image)htc.Controls[i];
                                htc.Controls.Remove(img);
                                htc.Controls.Add(litspace);
                            }
                            if (htc.Controls[i] is LiteralControl)
                            {
                                LiteralControl lit = (LiteralControl)htc.Controls[i];
                                //if (lit.Text != " " || lit.Text!="")
                               // {
                                string id = lit.ID;
                                string data = lit.Text;
                                LogWriter.WriteLog("LiteralControl=" + data);
                                if (data.ToLower().Contains("<a href"))
                                {
                                    htc.Controls.Remove(lit);
                                    htc.Controls.Add(litspace);
                                }

                                if (lit.ID != null)
                                {
                                    if (id.ToLower().Contains("reqimg_"))
                                    {
                                        htc.Controls.Remove(lit);
                                    }
                                }
                               // }
                            }
                            if (htc.Controls[i] is Literal)
                            {
                                Literal literal = (Literal)htc.Controls[i];
                                string litid = literal.ID;
                                string litclient = literal.ClientID;
                                string data1 =  literal.Text;
                                LogWriter.WriteLog("Literal=" + data1);
                                if (data1 != null)
                                {
                                    if (data1.ToLower().Contains("<a href") || data1.ToLower().Contains("<img"))
                                    {
                                        literal.Visible = false;
                                        //htc.Controls.RemoveAt(i);
                                        
                                        //htc.Controls.AddAt(i, litspace);
                                        //htc.Controls.Remove(literal);
                                        //htc.Controls.Add(litspace);
                                    }

                                }
                                if(litid!=null)
                                {
                                if (litid.ToLower().Contains("reqimg_"))
                                {
                                    htc.Controls.Remove(literal);
                                }
                                if (litid.ToLower().Contains("ll_div"))
                                {
                                    htc.Controls.Remove(literal);
                                }
                                }
                                
                            }
                            if (htc.Controls[i] is DropDownList)
                            {
                                DropDownList ddlist = (DropDownList)htc.Controls[i];
                                Label label1 = new Label();
                                label1.Font.Size = FontUnit.Point(9);
                                label1.CssClass = "defaultFont";
                                //label1.CssClass = "controlfonr";
                                label1.Text = ddlist.SelectedItem.Text;
                                ddlist.Visible = false;
                               // htc.Controls.Remove(ddlist);
                                htc.Controls.Add(label1);
                                htc.Controls.Add(litspace);
                            }
                            if (htc.Controls[i] is HtmlImage)
                            {
                                HtmlImage htimg = (HtmlImage)htc.Controls[i];
                                htc.Controls.Remove(htimg);
                            }
                            if (htc.Controls[i] is CheckBox)
                            {
                                CheckBox chk = (CheckBox)htc.Controls[i];
                                Label lb1 = new Label();
                                //lb1.CssClass = "controlFont";
                                lb1.CssClass = "defaultFont";
                                lb1.Font.Size = FontUnit.Point(9);
                                lb1.Text = chk.Checked.ToString();
                                htc.Controls.Remove(chk);
                                htc.Controls.Add(lb1);
                                htc.Controls.Add(litspace);
                            }

                            if (htc.Controls[i] is HtmlTable)
                            {
                                HtmlTable htmltb = (HtmlTable)htc.Controls[i];
                                if (htmltb.Controls.Count > 0)
                                {
                                    foreach (HtmlTableRow htmltr in htmltb.Rows)
                                    {
                                        if (htmltr.Controls.Count > 0)
                                        {
                                            foreach (HtmlTableCell htmltc in htmltr.Cells)
                                            {

                                                if (htmltc.HasControls())
                                                {
                                                    for (int m = 0; m < htmltc.Controls.Count; m++)
                                                    {
                                                        if (htmltc.Controls[m] is Label)
                                                        {
                                                            Label lbl1 = (Label)htmltc.Controls[m];
                                                            lbl1.CssClass = "defaultFont";
                                                            lbl1.Font.Size = FontUnit.Point(9);
                                                            string labelid = lbl1.ID;
                                                            if (labelid != null)
                                                            {
                                                                if (labelid.ToLower().Contains("lb_p_"))
                                                                {

                                                                    lbl1.Font.Bold = true;
                                                                }
                                                                if (lbl1.Text.ToLower().Contains("<a"))
                                                                {
                                                                    lbl1.Visible = false;
                                                                }
                                                            }
                                                            //lbl1.CssClass = "controlFont";
                                                            htmltc.Controls.Add(litspace);
                                                        }
                                                        if (htmltc.Controls[m] is TextBox)
                                                        {
                                                            Label mylabel = new Label();
                                                            //mylabel.CssClass = "controlFont";
                                                            mylabel.CssClass = "defaultFont";
                                                            mylabel.Font.Size = FontUnit.Point(9);
                                                            TextBox txt = (TextBox)htmltc.Controls[m];
                                                            txt.Visible = false;
                                                            mylabel.Text = txt.Text;
                                                            //htc.Controls.Remove(txt);
                                                            htmltc.Controls.Add(mylabel);
                                                            htmltc.Controls.Add(litspace);
                                                        }
                                                        if (htmltc.Controls[m] is RequiredFieldValidator)
                                                        {
                                                            RequiredFieldValidator reqval = (RequiredFieldValidator)htmltc.Controls[m];

                                                            string strreqval = reqval.ID;
                                                            if (strreqval != null)
                                                            {
                                                                if (strreqval.ToLower().Contains("rfv_"))
                                                                {
                                                                    htmltc.Controls.Remove(reqval);
                                                                }
                                                            }


                                                        }
                                                        if (htmltc.Controls[m] is ValidatorCalloutExtender)
                                                        {
                                                            ValidatorCalloutExtender valcall = (ValidatorCalloutExtender)htmltc.Controls[m];
                                                            string strvalcall = valcall.ID;
                                                            if (strvalcall != null)
                                                            {
                                                                if (strvalcall.ToLower().Contains("vce_"))
                                                                {
                                                                    htmltc.Controls.Remove(valcall);
                                                                }
                                                            }
                                                        }
                                                        if (htmltc.Controls[m] is Image)
                                                        {
                                                            Image img = (Image)htmltc.Controls[m];
                                                            htmltc.Controls.Remove(img);
                                                            htmltc.Controls.Add(litspace);
                                                        }
                                                        if (htmltc.Controls[m] is LiteralControl)
                                                        {
                                                            LiteralControl lit = (LiteralControl)htmltc.Controls[m];
                                                            //if (lit.Text != " " || lit.Text!="")
                                                            // {
                                                            string id = lit.ID;
                                                            string data = lit.Text;
                                                            if (data.ToLower().Contains("<a href"))
                                                            {
                                                                htmltc.Controls.Remove(lit);
                                                                htmltc.Controls.Add(litspace);
                                                            }

                                                            if (lit.ID != null)
                                                            {
                                                                if (id.ToLower().Contains("reqimg_"))
                                                                {
                                                                    htmltc.Controls.Remove(lit);
                                                                }
                                                            }
                                                            // }
                                                        }
                                                        if (htmltc.Controls[m] is Literal)
                                                        {
                                                            Literal literal = (Literal)htmltc.Controls[m];
                                                            string litid = literal.ID;
                                                            string litclient = literal.ClientID;
                                                            string data1 = literal.Text;
                                                            if (data1 != null)
                                                            {
                                                                if (data1.ToLower().Contains("<a href"))
                                                                {
                                                                    literal.Visible = false;
                                                                    //htc.Controls.RemoveAt(i);

                                                                    //htc.Controls.AddAt(i, litspace);
                                                                    //htc.Controls.Remove(literal);
                                                                    //htc.Controls.Add(litspace);
                                                                }
                                                            }
                                                            if (litid != null)
                                                            {
                                                                if (litid.ToLower().Contains("reqimg_"))
                                                                {
                                                                    htmltc.Controls.Remove(literal);
                                                                }
                                                                if (litid.ToLower().Contains("ll_div"))
                                                                {
                                                                    htmltc.Controls.Remove(literal);
                                                                }
                                                            }

                                                        }
                                                        if (htmltc.Controls[m] is DropDownList)
                                                        {
                                                            DropDownList ddlist = (DropDownList)htmltc.Controls[m];
                                                            Label label1 = new Label();
                                                            label1.Font.Size = FontUnit.Point(9);
                                                            label1.CssClass = "defaultFont";
                                                            //label1.CssClass = "controlfonr";
                                                            label1.Text = ddlist.SelectedItem.Text;
                                                            ddlist.Visible = false;
                                                            // htc.Controls.Remove(ddlist);
                                                            htmltc.Controls.Add(label1);
                                                            htmltc.Controls.Add(litspace);
                                                        }
                                                        if (htmltc.Controls[m] is HtmlImage)
                                                        {
                                                            HtmlImage htimg = (HtmlImage)htmltc.Controls[m];
                                                            htmltc.Controls.Remove(htimg);
                                                        }
                                                        if (htmltc.Controls[m] is CheckBox)
                                                        {
                                                            CheckBox chk = (CheckBox)htmltc.Controls[m];
                                                            Label lb1 = new Label();
                                                            //lb1.CssClass = "controlFont";
                                                            lb1.CssClass = "defaultFont";
                                                            lb1.Font.Size = FontUnit.Point(9);
                                                            lb1.Text = chk.Checked.ToString();
                                                            htmltc.Controls.Remove(chk);
                                                            htmltc.Controls.Add(lb1);
                                                            htmltc.Controls.Add(litspace);
                                                        }

                                                        if (htmltc.Controls[m] is HtmlTable)
                                                        {
                                                            
                                                        }
                                                        else
                                                        {
                                                            if (htmltc.Controls[m].ID != null)
                                                            {
                                                                string divid = htmltc.Controls[m].ID;
                                                                if (divid != null)
                                                                {
                                                                    if (divid.ToLower().Contains("div_"))
                                                                    {
                                                                        htmltc.Controls.RemoveAt(m);
                                                                    }
                                                                    if (divid.ToLower().Contains("search_img"))
                                                                    {
                                                                        htmltc.Controls.RemoveAt(m);
                                                                    }
                                                                    //if (divid.ToLower().Contains("rfv_"))
                                                                    //{
                                                                    //  htc.Controls.RemoveAt(i);
                                                                    //}
                                                                    //if (divid.ToLower().Contains("vce_"))
                                                                    //{
                                                                    //  htc.Controls.RemoveAt(i);
                                                                    //}
                                                                }
                                                            }
                                                        }
                                                    }

                                                    //foreach (Control cn in htc.Controls)
                                                    //{
                                                    //    if (cn is TextBox)
                                                    //    {
                                                    //        TextBox txtobj = (TextBox)cn;

                                                    //        Label lblobj = new Label();
                                                    //        lblobj.Text = txtobj.Text;

                                                    //        htc.Controls.Remove(txtobj);
                                                    //        htc.Controls.Add(lblobj);
                                                    //    }
                                                    //}
                                                }
































                                            }
                                        }
                                    }

                                }
                            }
                            else
                            {
                                if (htc.Controls[i].ID != null)
                                {
                                    string divid = htc.Controls[i].ID;
                                    if (divid != null)
                                    {
                                        if (divid.ToLower().Contains("div_"))
                                        {
                                            htc.Controls.RemoveAt(i);
                                        }
                                        if (divid.ToLower().Contains("search_img"))
                                        {
                                            htc.Controls.RemoveAt(i);
                                        }
                                        //if (divid.ToLower().Contains("rfv_"))
                                        //{
                                        //  htc.Controls.RemoveAt(i);
                                        //}
                                        //if (divid.ToLower().Contains("vce_"))
                                        //{
                                        //  htc.Controls.RemoveAt(i);
                                        //}
                                    }
                                }
                            }
                        }

                        //foreach (Control cn in htc.Controls)
                        //{
                        //    if (cn is TextBox)
                        //    {
                        //        TextBox txtobj = (TextBox)cn;

                        //        Label lblobj = new Label();
                        //        lblobj.Text = txtobj.Text;

                        //        htc.Controls.Remove(txtobj);
                        //        htc.Controls.Add(lblobj);
                        //    }
                        //}
                    }
                }
            }


            //foreach (Control htrcon in table.Controls)
            //{

            //    if (htrcon is HtmlTableRow)
            //    {
            //        foreach(Control htccon in htrcon.Controls)
            //        {
            //            if(htccon is HtmlTableCell)
            //            {
            //                foreach (Control con in htccon.Controls)
            //                {
            //                    if (con is TextBox)
            //                    {
            //                        TextBox txtobj = (TextBox)con;
                                    
            //                        Label lblobj = new Label();
            //                        lblobj.Text = txtobj.Text;
            //                        txtobj.Text = "testing data";
            //                        txtobj.Visible = false;

            //                        //htccon.Controls.Remove(txtobj);

            //                     //   htccon.Controls.Add(lblobj);

            //                    }
            //                }
            //            }
            //        }
            //    }



                
            
           // }
            tdmain.Controls.Add(table);



            HtmlTableCell htmltablecell = (HtmlTableCell)form1.FindControl("tdDisplayGrid");
            for(int i=0;i<htmltablecell.Controls.Count;i++)
            {
                if (htmltablecell.Controls[i] is HtmlTable)
                {
                    HtmlTable ht1 = (HtmlTable)htmltablecell.Controls[i];

                    ht1.Border = 1;
                    ht1.BorderColor = "Black";
                    ht1.CellPadding = 0;
                    ht1.CellSpacing=0;

                    foreach (HtmlTableRow htr1 in ht1.Rows)
                    {
                        foreach (HtmlTableCell htc1 in htr1.Cells)
                        {
                            if (htc1.Controls.Count > 0)
                            {
                                for (int j = 0; j < htc1.Controls.Count; j++)
                                {
                                    if (htc1.Controls[j] is Button)
                                    {
                                        Button button1 = (Button)htc1.Controls[j];
                                        button1.Visible = false;
                                        //htc1.Controls.Remove(button1);
                                        //htc1.Controls.Add(litspace);
                                        htc1.Width = "0%";
                                        htc1.Height = "0%";
                                    }
                                    if (htc1.Controls[j] is HtmlInputButton)
                                    {
                                        HtmlInputButton button1 = (HtmlInputButton)htc1.Controls[j];
                                        button1.Visible = false;
                                        //htc1.Controls.Remove(button1);
                                        //htc1.Controls.Add(litspace);
                                        htc1.Width = "0%";
                                        htc1.Height = "0%";
                                    }
                                    if (htc1.Controls[j] is LinkButton)
                                    {
                                        LinkButton linkbutton2 = (LinkButton)htc1.Controls[j];
                                        Label label2 = new Label();
                                        label2.CssClass = "defaultFont";
                                        label2.Font.Size = FontUnit.Point(9);
                                        label2.Font.Bold = true;
                                        label2.Text = linkbutton2.Text;
                                        htc1.Controls.Remove(linkbutton2);
                                        htc1.Controls.Add(label2);
                                        htc1.Controls.Add(litspace);
                                        //htc1.Align = "center";
                                    }
                                    if (htc1.Controls[j] is HtmlTable)
                                    {
                                        HtmlTable htmltable1 = (HtmlTable)htc1.Controls[j];
                                        htmltable1.Border = 0;
                                        htmltable1.CellSpacing = 0;
                                        htmltable1.CellPadding = 0;
                                        htc1.Width = "0%";
                                        foreach (HtmlTableRow htmltablerow1 in htmltable1.Rows)
                                        {
                                            foreach (HtmlTableCell htmltablecell1 in htmltablerow1.Cells)
                                            {
                                                // added by srivatsan on Mar 21
                                                // htmltablerow1.Cells[0].Visible = false;
                                                if (htmltablecell1.HasControls())
                                                {

                                                    for (int k = 0; k < htmltablecell1.Controls.Count; k++)
                                                    {
                                                        if (htmltablecell1.Controls[k] is LinkButton)
                                                        {
                                                            LinkButton linkbut1 = (LinkButton)htmltablecell1.Controls[k];
                                                            Label mylabel = new Label();
                                                            mylabel.Font.Size = FontUnit.Point(9);
                                                            mylabel.CssClass = "defaultFont";
                                                            mylabel.Text = linkbut1.Text;
                                                            htmltablecell1.Controls.Remove(linkbut1);
                                                            htmltablecell1.Controls.Add(mylabel);
                                                            htmltablecell1.Width = "0px";
                                                        }
                                                        if (htmltablecell1.Controls[k] is Label)
                                                        {
                                                            Label label5 = (Label)htmltablecell1.Controls[k];
                                                            label5.Visible = false;



                                                            // htmltablecell1.Controls.Remove(linkbut1);
                                                            //htmltablecell1.Controls.Add(mylabel);
                                                        }
                                                        if (htmltablecell1.Controls[k] is ImageButton)
                                                        {
                                                            ImageButton imgbutton = (ImageButton)htmltablecell1.Controls[k];

                                                            imgbutton.Visible = false;
                                                            htmltablecell1.Visible = false;
                                                            htmltablecell1.Width = "0px";
                                                            //htmltablecell1.Controls.Remove(imgbutton);
                                                            //htc1.Controls.Add(litspace);
                                                        }
                                                        if (htmltablecell1.Controls[k] is Button)
                                                        {
                                                            Button button = (Button)htmltablecell1.Controls[k];
                                                            button.Visible = false;
                                                            htmltable1.Visible = false;
                                                            //htmltablecell1.Controls.Remove(button);
                                                            // htc1.Controls.Add(litspace);
                                                        }
                                                        if (htmltablecell1.Controls[k] is HtmlInputButton)
                                                        {
                                                            HtmlInputButton button = (HtmlInputButton)htmltablecell1.Controls[k];
                                                            button.Visible = false;
                                                            htmltable1.Visible = false;
                                                            //htmltablecell1.Controls.Remove(button);
                                                            // htc1.Controls.Add(litspace);
                                                        }
                                                        if (htmltablecell1.Controls[k] is Literal)
                                                        {
                                                            Literal literal3 = (Literal)htmltablecell1.Controls[k];
                                                            literal3.Text = "";
                                                            htc1.Controls.Remove(literal3);
                                                            htc1.Controls.Add(litspace);

                                                        }
                                                        if (htmltablecell1.Controls[k] is LiteralControl)
                                                        {
                                                            LiteralControl literalcontrol3 = (LiteralControl)htmltablecell1.Controls[k];
                                                            literalcontrol3.Text = "";
                                                            htc1.Controls.Remove(literalcontrol3);
                                                            htc1.Controls.Add(litspace);
                                                        }
                                                        if (htmltablecell1.Controls[k] is HtmlTable)
                                                        {
                                                            HtmlTable ht = (HtmlTable)htmltablecell1.Controls[k];
                                                            htc1.Controls.Remove(ht);
                                                            htc1.Controls.Add(litspace);
                                                        }
                                                        if (htmltablecell1.Controls[k] is Label)
                                                        {
                                                            Label lbl2 = (Label)htmltablecell1.Controls[k];
                                                            lbl2.Font.Size = FontUnit.Point(9);
                                                            lbl2.CssClass = "defaultFont";
                                                            lbl2.Font.Bold = true;
                                                            string lbltxt = lbl2.ID;
                                                            string strlbl = lbl2.Text;

                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    if (htc1.Controls[j] is Literal)
                                    {
                                        Literal lite1 = (Literal)htc1.Controls[j];
                                        string name = lite1.ID;
                                        if (lite1.Text.ToLower().Contains("<a href"))
                                        {
                                            string mystirng = lite1.Text;
                                        }
                                    }
                                    if (htc1.Controls[j] is LiteralControl)
                                    {
                                        LiteralControl litecon1 = (LiteralControl)htc1.Controls[j];
                                        Label mylabel1 = new Label();
                                        mylabel1.CssClass = "defaultFont";
                                        mylabel1.Font.Size = FontUnit.Point(9);
                                        mylabel1.Text = litecon1.Text;
                                        string name1 = litecon1.ID;
                                        if (litecon1.Text.ToLower().Contains("<a href"))
                                        {
                                            string mystirng1 = litecon1.Text;
                                            int indexstart = mystirng1.IndexOf("\"blue\">");
                                            indexstart += 7;

                                            string newstring = mystirng1.Substring(indexstart);
                                            int indexend = mystirng1.IndexOf("</font>");
                                            string newstring1 = newstring.Replace("</font></a>&nbsp;", "");
                                            //mystirng1 = mystirng1.Substring(

                                            litecon1.Text = newstring1;
                                            mylabel1.Text = litecon1.Text;
                                        }

                                        if (litecon1.Text != "&nbsp;")
                                        {
                                            htc1.Controls.Remove(litecon1);
                                            htc1.Controls.Add(mylabel1);
                                        }
                                        else if (litecon1.Text == "&nbsp;")
                                        {
                                            litecon1.Visible = false;
                                            htc1.Visible = false;
                                        }

                                    }
                                    if (htc1.Controls[j] is Image)
                                    {
                                        Image img1 = (Image)htc1.Controls[j];
                                        string name = img1.ID;
                                        img1.Visible = false;
                                    }
                                    if (htc1.Controls[j] is HtmlImage)
                                    {
                                        HtmlImage img2 = (HtmlImage)htc1.Controls[j];
                                        string name = img2.ID;
                                        img2.Visible = false;
                                    }
                                }
                            }
                            else
                            {
                                htc1.Visible = false;
                            }
                        }

                    }
                }
            }



        }
        catch (Exception ex)
        {

        }


    }

    
    public Control FindControl(ControlCollection frmControl, string strControlId)
    {
        Control cRet = null;
        foreach (Control con in frmControl)
        {
            if (con.ID != null)
                if (con.ID.ToLower() == strControlId.ToLower())
                    return con;
        }

        foreach (Control cn in frmControl)
            if (cn.Controls.Count > 0)
            {
                cRet = FindControl(cn.Controls, strControlId);
                if (cRet != null) break;
            }
        return cRet;
    }


    public Control FindControlbyType(ControlCollection frmControl, string strControltype)
    {

        Control cRet = null;
        foreach (Control con in frmControl)
        {
            if (con.ID != null)
                if (con.GetType().Name.ToLower() == strControltype.ToLower()) 
                    return con;
        }

        foreach (Control cn in frmControl)
            if (cn.Controls.Count > 0)
            {
                cRet = FindControl(cn.Controls, strControltype);
                if (cRet != null) break;
            }
        return cRet;
    }

    private void setPageTitle()
    {
        try
        {
            if (Session["pageTitle"] != null)
            {
                Page.Header.Title = Session["pageTitle"].ToString();
            }
            else
            {
                if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(".") + "\\maxbloxtitle.ini"))
                {
                    System.IO.StreamReader sr = new System.IO.StreamReader(HttpContext.Current.Server.MapPath(".") + "\\maxbloxtitle.ini");
                    Session["pageTitle"] = sr.ReadLine();
                    Page.Header.Title = Session["pageTitle"].ToString();
                    sr.Close();
                    sr = null;
                }
            }
        }
        catch (Exception e)
        {
            LogWriter.WriteLog("Error in setPageTitle function. Error: " + e.Message);
        }
    }

    private void setLogo()
    {
        if (IsPostBack) return;//logic for transparent and large images
        ltrl_logo.Text = "<div class='backs'></div>";
        string temp = "images/LightBlue/logo.gif";
        DynamicDisplayI ddi = new DynamicDisplayI();
        HDDatabase hddLogo = null;
        security s = null;
        try
        {
            if (Session["company_afrowid"] != null)
            {
                DirectoryInfo di = new DirectoryInfo(Server.MapPath(".") + "\\Images\\CustomLogo");
                FileInfo[] fis = di.GetFiles(Session["company_afrowid"].ToString() + "*");
                if (fis.Length > 0)
                {
                    temp = "images/CustomLogo/" + fis[0].Name + "";
                    //ltrl_logo.Text = "<img src='Images/CustomLogo/" + fis[0].Name + "' />";
                    ltrl_logo.Text = "<table border='0' cellpadding='0' cellspacing='0' width='236' height='76'><tr><td><img src=images/CustomLogo/" + fis[0].Name + "></img></td></tr></table>";
                }
                else//if another user in the same company logs out
                {
                    if (Session["hasCustomLogo"] != null)
                    {
                        string fName = ""; string ext = "";
                        s = new security();
                        s.createCustomLogo(ref fName, ref ext);
                        //ltrl_logo.Text = "<table border='0' cellpadding='0' cellspacing='0' width='236' height='76'><tr><td style=background:url('images/CustomLogo/" + Session["company_afrowid"] + ext + "');></td></tr></table>";
                        ltrl_logo.Text = "<table border='0' cellpadding='0' cellspacing='0' width='236' height='76'><tr><td><img src=images/CustomLogo/" + Session["company_afrowid"] + ext + "><img></td></tr></table>";
                    }
                    else
                    {
                        //ltrl_logo.Text = "<table border='0' cellpadding='0' cellspacing='0' width='236' height='76'><tr><td style=background:"+temp+"></td></tr></table>";
                        ltrl_logo.Text = "<table border='0' cellpadding='0' cellspacing='0' width='236' height='76'><tr><td ><img src=Images/logo.GIF></img></td></tr></table>";
                    }
                }
            }
            //HtmlTableCell htc = (HtmlTableCell)ddi.FindControl(this.Controls, "td_logo");
            //htc.Style.Add("background",temp);
        }
        catch (Exception e)
        {

        }
        finally
        {
            hddLogo = null;
            ddi = null;
            s = null;
        }
    }
    private string wrapItAsMessage(string msg, bool isError)
    {
        return "<div class=\"" + (isError == true ? "mbErrorMessage" : "mbMessage") + "\">" + msg + "</div>";
    }
}
